package com.sparrow.manage.company;

import com.sparrow.manage.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CompanyServiceImpl extends BaseServiceImpl<Company, String> implements CompanyService {

    private static String companyName = null;

    private static String companySimpleName = null;

    @Autowired
    private CompanyDao companyDao;

    @Override
    protected JpaRepository<Company, String> getDao() {
        return this.companyDao;
    }

    @Override
    public Company insert(Company company) {
        // 不能插入新的记录
        return null;
    }

    @Override
    public void delete(String id) {
        // 不能删除记录
    }

    @Override
    public Company updateById(Company company) {
        company.setUpdateTime(new Date());
        Company record = this.queryOne();
        if(record != null){
            company.setId(record.getId());
        }
        companyName = null;
        return companyDao.save(company);
    }

    @Override
    public Company queryOne() {
        List<Company> companies = companyDao.findAll();
        if(companies == null || companies.isEmpty()) return null;
        else{
            return companies.get(0);
        }
    }

    @Override
    public String companyName() {
        Company company = this.queryOne();
        if(company != null){
            companyName = company.getName();
        }
        return companyName == null ? "" : companyName;
    }

    @Override
    public String companySimpleName() {
        Company company = this.queryOne();
        if(company != null){
            companySimpleName = company.getSname();
        }
        return companySimpleName == null ? "" : companySimpleName;
    }
}
